/* dup2test.c */
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <errno.h>

void main()
{
    int newfd, oldfd1, oldfd2, newfd2, nchar;
    char buf[30];
    char *file1 = "a.txt";
    char *file2 = "b.txt";

    oldfd1 = open(file1, O_RDWR | O_CREAT, 0666);
    oldfd2 = open(file2, O_RDWR | O_CREAT, 0666);

    if (oldfd1 > 0)
        printf("The oldfd1 file descriptor =%d\n", oldfd1);
    if (oldfd2 > 0)
        printf("The oldfd2 file descriptor =%d\n", oldfd2);

    newfd = dup(oldfd1);
    printf("The newfd file descriptor =%d\n", newfd);

    close(0); // 关闭标准输入（描述符 0）
    newfd2 = dup2(oldfd1, 0); // 将 oldfd1 复制到描述符 0
    if (newfd2 == -1) {
        perror("dup2 failed\n");
        exit(0);
    }
    printf("The newfd2 file descriptor =%d\n", newfd2);

    nchar = read(newfd2, buf, 28);
    if (nchar == -1) {
        perror("read failed\n");
        exit(0);
    }
    buf[nchar] = '\0';
    printf("I have read from a.txt:%s\n", buf);
}
